package org.cpoy.ei.session;

import org.cpoy.eims.entity.*;
import java.util.ArrayList;
import java.util.List;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

@Name("parentHome")
public class ParentHome extends EntityHome<Parent> {

	@In(create = true)
	UserAccountHome userAccountHome;
	@In(create = true)
	StudentHome studentHome;
	@In(create = true)
	RelationshipHome relationshipHome;
	@In(create = true)
	AddressHome addressHome;

	public void setParentId(Integer id) {
		setId(id);
	}

	public Integer getParentId() {
		return (Integer) getId();
	}

	@Override
	protected Parent createInstance() {
		Parent parent = new Parent();
		return parent;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		UserAccount userAccount = userAccountHome.getDefinedInstance();
		if (userAccount != null) {
			getInstance().setUserAccount(userAccount);
		}
		Student student = studentHome.getDefinedInstance();
		if (student != null) {
			getInstance().setStudent(student);
		}
		Relationship relationship = relationshipHome.getDefinedInstance();
		if (relationship != null) {
			getInstance().setRelationship(relationship);
		}
		Address address = addressHome.getDefinedInstance();
		if (address != null) {
			getInstance().setAddress(address);
		}
	}

	public boolean isWired() {
		if (getInstance().getStudent() == null)
			return false;
		if (getInstance().getAddress() == null)
			return false;
		return true;
	}

	public Parent getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

	public List<StudentAdmission> getStudentAdmissions() {
		return getInstance() == null ? null : new ArrayList<StudentAdmission>(
				getInstance().getStudentAdmissions());
	}

	public List<StudentEmergencyContact> getStudentEmergencyContacts() {
		return getInstance() == null ? null
				: new ArrayList<StudentEmergencyContact>(getInstance()
						.getStudentEmergencyContacts());
	}

}
